package com.czn.shopdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czn.shopdemo.mapper.CartMapper;
import com.czn.shopdemo.model.Cart;
import com.czn.shopdemo.model.Goods;
import com.czn.shopdemo.model.enums.ResultCodeEnum;
import com.czn.shopdemo.model.exception.CustomException;
import com.czn.shopdemo.service.CartService;
import com.czn.shopdemo.service.GoodsService;
import com.czn.shopdemo.utils.TokenInfoUtil;
import com.czn.shopdemo.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * @author Zenan Chen
 * @date 2023/4/4 14:56
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CartMapper cartMapper;

    @Override
    public void addToCart(Cart cart, HttpServletRequest request) {
        // 校验加入购物车的数量
        Integer count = cart.getCount();
        String goodsId = cart.getGoodsId();
        Goods goods = goodsService.getGoodDetail(goodsId);
        if (count <= 0 || null == goods) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        if (cart.getCount() > goods.getNum()) {
            throw new CustomException("加入购物车的数量超过库存上线！", 211);
        }
        // 新增
        if (Utils.isEmptyStr(cart.getUuid())) {
            // 添加到购物车
            cart.setGoods(goods);
            Long userId = TokenInfoUtil.getUserId(request);
            cart.setUserId(String.valueOf(userId));
            cart.setCreateTime(new Date());
            cart.setUpdateTime(new Date());
            cart.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
            cartMapper.insert(cart);
            // 更新
        } else {
            cart.setUpdateTime(new Date());
            LambdaQueryWrapper<Cart> updateWrapper = Wrappers.lambdaQuery(Cart.class).eq(Cart::getUuid, cart.getUuid());
            cartMapper.update(cart, updateWrapper);
        }
    }

    @Override
    public Page<Cart> getCartGoodsList(Integer pageNo, Integer pageSize, HttpServletRequest request) {
        Page<Cart> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Cart> queryWrapper = Wrappers.lambdaQuery(Cart.class).eq(Cart::getIsDeleted, "0").eq(Cart::getUserId, TokenInfoUtil.getUserId(request));
        Page<Cart> cartPage = cartMapper.selectPage(page, queryWrapper);
        cartPage.getRecords().stream().forEach(cart -> fillCart(cart));
        return cartPage;
    }

    @Override
    public void deleteCartGoods(String cartId) {
        LambdaQueryWrapper<Cart> wrapper = Wrappers.lambdaQuery(Cart.class).eq(Cart::getUuid, cartId);
        cartMapper.delete(wrapper);
    }

    @Override
    public Cart getCartGoodsDetail(String uuid) {
        LambdaQueryWrapper<Cart> queryWrapper = Wrappers.lambdaQuery(Cart.class).eq(Cart::getUuid, uuid);
        Cart cart = cartMapper.selectOne(queryWrapper);
        fillCart(cart);
        return cart;
    }

    private void fillCart(Cart cart) {
        Goods goods = goodsService.getGoodDetail(cart.getGoodsId());
        cart.getParam().put("goods", goods);
    }

}
